﻿/// <reference path="/Js/Jquery/jquery-1.4.1-vsdoc.js" />
/// <reference path="/Js/Popup/Loading.js" />
/// <reference path="/Js/Jquery/jquery-jtemplates.js" />
/// <reference path="/plugin/alert/js/jquery.alerts.js" />
/// <reference path="/js/Common/vtc-paging.js" />
/// <reference path="/js/Common/Message.js" />
Pages_ListLog = new function () {
    this.htmlTag = new function () {
        this.divLog = '#divLog';
        this.divSearch = '#divSearch';
        this.txtKeyWord = '#txtKeyWord';
        this.ddlFunction = '#ddlFunction';
        this.ddlAction = '#ddlAction';
        this.ddlServicesID = "#ddlServicesID";
        this.ddlUser = '#ddlUser';
        this.btnSearch = '#btnSearch';
        this.divList = '#divList';
        this.divPaging = '#divPaging';
        this.txtFromDate = '#txtFromDate';
        this.txtToDate = '#txtToDate';
        this.lblTotalAction = "#lblTotalAction";
    };
    this.variable = new function () {
        this.m_toDate = '';
        this.m_fromDate = '';
        this.m_keyWord = '';
        this.m_function = -1;
        this.m_action = -1;
        this.m_userID = -1;
        this.m_servicesID = -1;
    };

    this.m_objPaging = new VtcPaging("divPaging", "Pages_ListLog.bindData", "pagingCss", 50, CONSTANT.PAGE_DISPLAY);

    this.pageLoad = function () {
        $(this.htmlTag.txtFromDate).datepicker({
            changeMonth: true,
            changeYear: true
        });
        $(this.htmlTag.txtToDate).datepicker({
            changeMonth: true,
            changeYear: true
        });

        var today = new Date();
        var m_day = today.getDate();
        var m_month = today.getMonth() + 1;
        var m_year = today.getFullYear();
        if (m_day < 10) m_day = '0' + m_day;
        if (m_month < 10) m_month = '0' + m_month;

        var m_fromDay = m_day;
        var m_fromMonth = m_month;
        var m_fromYear = m_year - 1;
        if (m_fromDay > 28) {
            m_fromDay = 28;
        }

        this.variable.m_fromDate = m_fromDay + "/" + m_fromMonth + "/" + m_fromYear;
        this.variable.m_toDate = m_day + "/" + m_month + "/" + m_year;

        $(this.htmlTag.txtFromDate).val(this.variable.m_fromDate);
        $(this.htmlTag.txtToDate).val(this.variable.m_toDate);
        this.bindFunction();
        this.bindddlUser();
        this.bindData(1, this.m_objPaging.pageSize);
        $(this.htmlTag.txtKeyWord, this.htmlTag.divLog).focus();
    };
    // Lấy danh sách sản phẩm
    this.bindData = function (m_curentPage, m_pageSize) {
        Pages_ListLog.variable.m_keyWord = $(Pages_ListLog.htmlTag.txtKeyWord, Pages_ListLog.htmlTag.divLog).val();
        Pages_ListLog.variable.m_userID = $(Pages_ListLog.htmlTag.ddlUser, Pages_ListLog.htmlTag.divLog).val();
        Pages_ListLog.variable.m_function = $(Pages_ListLog.htmlTag.ddlFunction, Pages_ListLog.htmlTag.divLog).val();
        Pages_ListLog.variable.m_action = $(Pages_ListLog.htmlTag.ddlAction, Pages_ListLog.htmlTag.divLog).val();
        Pages_ListLog.variable.m_servicesID = $(Pages_ListLog.htmlTag.ddlServicesID, Pages_ListLog.htmlTag.divLog).val();
        Pages_ListLog.variable.m_fromDate = $(Pages_ListLog.htmlTag.txtFromDate, Pages_ListLog.htmlTag.divLog).val();
        Pages_ListLog.variable.m_toDate = $(Pages_ListLog.htmlTag.txtToDate, Pages_ListLog.htmlTag.divLog).val();
        if (!Pages_ListLog.validateSearch()) return;
        Loading.showProcess();
        $.ajax({
            type: "GET",
            url: "/Handler/HandlerConfig.ashx",
            data: { t: 'GetListLog',
                keyWord: Pages_ListLog.variable.m_keyWord,
                adminID: Pages_ListLog.variable.m_userID,
                functionID: Pages_ListLog.variable.m_function,
                action: Pages_ListLog.variable.m_action,
                servicesID: Pages_ListLog.variable.m_servicesID,
                fromDate: Pages_ListLog.variable.m_fromDate,
                toDate: Pages_ListLog.variable.m_toDate,
                currentPage: m_curentPage,
                pageSize: m_pageSize
            },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                Pages_ListLog.processData(data, m_curentPage);
                Loading.closeProcess();
            }
        });
    };

    this.processData = function (data, _cur) {
        $(this.htmlTag.lblTotalAction).html('(' + COMMON.formatMoney(data.TotalRecord) + ')');
        $(this.htmlTag.divList, this.htmlTag.divLog).setTemplateURL("/Template/Pages/Config/ListLog.htm");
        $(this.htmlTag.divList, this.htmlTag.divLog).processTemplate(data);
        this.m_objPaging.bindPaging(_cur, data.TotalRecord);
        if (data.TotalRecord <= this.m_objPaging.pageSize) {
            $(this.htmlTag.divPaging, this.htmlTag.divLog).hide();
        }
        else {
            $(this.htmlTag.divPaging, this.htmlTag.divLog).show();
        }
    };

    // Kiem tra du lieu truoc khi tim kiem
    this.validateSearch = function () {
        if (Pages_ListLog.variable.m_fromDate == '') {
            jAlert(MESSAGE.datetimenull, MESSAGE.title, function () {
                $(Pages_ListLog.htmlTag.txtFromDate).focus();
            });
            return false;
        }
        if (Pages_ListLog.variable.m_toDate == '') {
            jAlert(MESSAGE.datetimenull, MESSAGE.title, function () {
                $(Pages_ListLog.htmlTag.txtToDate).focus();
            });
            return false;
        }

        if (Untils.isDate(Pages_ListLog.variable.m_fromDate) < 0) {
            jAlert(MESSAGE.errorFormatDate, MESSAGE.title, function () {
                $(Pages_ListLog.htmlTag.txtFromDate).focus();
            });
            return false;
        }

        if (Untils.isDate(Pages_ListLog.variable.m_toDate) < 0) {
            jAlert(MESSAGE.errorFormatDate, MESSAGE.title, function () {
                $(Pages_ListLog.htmlTag.txtToDate).focus();
            });
            return false;
        }
        if (!Untils.DateCompare(Pages_ListLog.variable.m_fromDate, Pages_ListLog.variable.m_toDate)) {
            jAlert(MESSAGE.erroDateTime, MESSAGE.title, function () {
                $(Pages_ListLog.htmlTag.txtToDate).focus();
            });
            return false;
        }
        return true;
    };

    this.bindFunction = function () {
        $.ajax({
            type: "GET",
            url: "/handler/HandlerConfig.ashx",
            data: { t: 'GetListFunctionForLog' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                if (typeof (data) != 'undefined') {
                    var _sHtml = '<option value="-1">[--' + MESSAGE.all + '--]</option>';
                    for (var i = 0; i < data.Items.length; i++) {
                        if ($.browser.msie) {
                            _sHtml += '<option value="' + data.Items[i].Info.FunctionID + '">' + data.Items[i].Info.Name + '</option>';
                        }
                        else {

                            _sHtml += '<option style="font-weight:bold" value="' + data.Items[i].Info.Id + '">' + data.Items[i].Info.Name + '</option>';
                        }
                        for (var j = 0; j < data.Items[i].Items.length; j++) {
                            if ($.browser.msie) {
                                _sHtml += '<option value="' + data.Items[i].Items[j].Id + '">&nbsp;&nbsp;&nbsp;' + data.Items[i].Items[j].Name + '</option>';
                            }
                            else {
                                _sHtml += '<option style="padding-left:20px;" value="' + data.Items[i].Items[j].Id + '">' + data.Items[i].Items[j].Name + '</option>';
                            }
                        }
                    }
                    $(Pages_ListLog.htmlTag.ddlFunction).html(_sHtml);
                }
            }
        });
    };

    this.bindddlUser = function () {
        $.ajax({
            type: "GET",
            url: "/Handler/HandlerConfig.ashx",
            data: { t: "GetListAdminAll" },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                var _sHtml = '<option value="-1">[--' + MESSAGE.all + '--]</option>';
                for (var i = 0; i < data.TotalRecord; i++) {
                    _sHtml += '<option value="' + data.Items[i].Id + '">' + data.Items[i].UserName + '</option>';
                }
                $(Pages_ListLog.htmlTag.ddlUser).html(_sHtml);

            }
        });
    };

    $(this.htmlTag.btnSearch, this.htmlTag.divLog).live('click', function () {
        Pages_ListLog.bindData(1, Pages_ListLog.m_objPaging.pageSize);
    });

    this.getAction = function (_action) {
        var m_action = '';
        switch (_action) {
            case 'L': m_action = 'Đăng nhập'; break;
            case 'A': m_action = 'Thêm mới'; break;
            case 'M': m_action = 'Sửa'; break;
            case 'D': m_action = 'Xóa'; break;
            case 'C': m_action = 'Cập nhật trạng thái'; break;
        }
        return m_action;
    };
};